//
//  VSUtils.m
//  VStore
//
//  Created by Apple on 14/12/3.
//  Copyright (c) 2014年 LITB. All rights reserved.
//

#import "VSUtils.h"
#import <UIKit/UIKit.h>
#import "WXCKeychain.h"

@implementation VSUtils

+ (void)adaptiveUIForiPhone4:(void(^)(void))blockForiPhone4
                  foriPhone5:(void(^)(void))blockForiPhone5
                  foriPhone6:(void(^)(void))blockForiPhone6
              foriPhone6plus:(void(^)(void))blockForiPhone6plus
                     unknown:(void(^)(void))blockForUnknown
{
    NSInteger height = [[UIScreen mainScreen] bounds].size.height;
    if (height == 480 && blockForiPhone4)
    {
        blockForiPhone4();
        return;
    }
    if (height == 568 && blockForiPhone5)
    {
        blockForiPhone5();
        return;
    }
    if (height == 667 && blockForiPhone6)
    {
        blockForiPhone6();
        return;
    }
    if (height == 736 && blockForiPhone6plus)
    {
        blockForiPhone6plus();
        return;
    }
    if (blockForUnknown) {
        blockForUnknown();
    }
}

+ (NSInteger)screenWidth
{
    return [[UIScreen mainScreen] bounds].size.width;
}

+ (NSInteger)screenHeight
{
    return [[UIScreen mainScreen] bounds].size.height;
}

NSString * const KEY_IMEI = @"com.camera360.jigsaw";

+ (NSString*) deviceID
{
    NSString* deviceID = (NSString*)[WXCKeychain load:KEY_IMEI];
    if ([deviceID length] <= 0) {
        deviceID = [[NSUserDefaults standardUserDefaults]objectForKey:@"DEVICE_ID"];
    }
    if ([deviceID length] <= 0) {
        deviceID = [VSUtils generateUUIDString:nil];
        BOOL ret = [WXCKeychain save:KEY_IMEI data:deviceID];
        if (ret == NO) {
            [[NSUserDefaults standardUserDefaults]setObject:deviceID forKey:@"DEVICE_ID"];
            [[NSUserDefaults standardUserDefaults]synchronize];
        }
    }
    return deviceID;
}

+ (NSString*) generateUUIDString:(NSString*)addPrefixString
{
    CFUUIDRef       uuid    = NULL;
    CFStringRef     uuidStr = NULL;
    NSString *      result  = nil;
    
    uuid = CFUUIDCreate(kCFAllocatorDefault);
    if (uuid)
    {
        uuidStr = CFUUIDCreateString(kCFAllocatorDefault, uuid);
        if (uuidStr)
        {
            if (addPrefixString)
                result = [NSString stringWithFormat:@"%@%@", addPrefixString, uuidStr];
            else
                result = [NSString stringWithString:(__bridge NSString *)uuidStr];
        }
    }
    
    if (uuidStr) CFRelease(uuidStr);
    if (uuid) CFRelease(uuid);
    result = [result stringByReplacingOccurrencesOfString:@"-" withString:@""];
    return result;
}

@end
